﻿using DevExpress.XtraEditors;
using DevExpress.XtraNavBar;
using DevExpress.XtraTreeList;
using MM.Commmon.UI;
using MM.Common.Base;
using MM.Common.DBBase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MM.Common.Support
{
    public partial class FrmOrganization : FrmFunctionBase
    {
        public FrmOrganization()
        {
            InitializeComponent();
        }
        public bool delete(string id)
        {
            DataParamCol parms = new DataParamCol(new DataParam[] { new InputDataParam("did", id) });
            return (SystemInfo.DbData.IsOpen && (SystemInfo.DbData.ExecuteSql("delete " + this.table + " where " + this.key + "=:did", parms) == 1));
        }
        private void FrmOrganization_Load(object sender, EventArgs e)
        {
            this.getTreeData();
        }

        private void frmreg_AddToMain(object sender, EventArgs e)
        {
            this.getTreeData();
        }

        private void getTreeData()
        {
            string commandtext = string.Format("select * from {0} order by ORG_LEVEL,ORG_CODE", this.table);
            DataTable dt = new DataTable();
            if (SystemInfo.DbData.Retrieve(commandtext, ref dt))
            {
                this.treeOrganization.DataSource = dt;
            }
        }
        private void nbiDelete_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            if (XtraMessageBox.Show("确定要删除吗?", "提示", MessageBoxButtons.YesNo) != DialogResult.No)
            {
                string id = this.treeOrganization.FocusedNode.GetDisplayText(this.key).Trim();
                this.delete(id);
                this.getTreeData();
            }
        }

        private void nbiEdit_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            FrmOrganizationSetup setup = new FrmOrganizationSetup(this.treeOrganization.FocusedNode.GetDisplayText(this.key).Trim());
            setup.AddToMain += new FrmOrganizationSetup.AddToMainEventHandler(this.frmreg_AddToMain);
            setup.ShowDialog();
        }

        private void nbiExit_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            base.Close();
        }

        private void nbiNew_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            if (this.treeOrganization.FocusedNode.GetValue("ORG_IS_LEAF").ToString() == "Y")
            {
                MessageBox.Show("该节点为末级节点，不能新增子节点");
            }
            else
            {
                string parentId = this.treeOrganization.FocusedNode.GetValue(this.key).ToString().Trim();
                string code = this.treeOrganization.FocusedNode.GetValue("ORG_CODE").ToString().Trim();
                int num = Convert.ToInt32(this.treeOrganization.FocusedNode.GetValue("ORG_LEVEL"));
                int num2 = Convert.ToInt32(this.treeOrganization.FocusedNode.GetValue("ORG_LAYER"));
                if (num < 6)
                {
                    num++;
                }
                if (num2 < 4)
                {
                    num2++;
                }
                FrmOrganizationSetup setup = new FrmOrganizationSetup(parentId, code, num.ToString(), num2.ToString());
                setup.AddToMain += new FrmOrganizationSetup.AddToMainEventHandler(this.frmreg_AddToMain);
                setup.ShowDialog();
            }
        }

        private void nbiPrint_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            //if (PrintHelper.IsPrintingAvailable)
            //{
            //    PrintHelper.ShowPreview(this.treeOrganization);
            //}
        }

        private void nbiRegional_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            string code = this.treeOrganization.FocusedNode.GetValue("ORG_CODE").ToString();
            Convert.ToInt32(this.treeOrganization.FocusedNode.GetValue("ORG_LAYER"));
            this.frmOrgReg.Edit(code);
        }

        private void ShowHitInfo(TreeListHitInfo hi)
        {
            if (hi.Node != null)
            {
                this.checkNode = hi.Node.GetValue("ORG_ID").ToString();
            }
        }

        private void treeOrganization_DoubleClick(object sender, EventArgs e)
        {
            FrmOrganizationSetup setup = new FrmOrganizationSetup(this.checkNode);
            setup.AddToMain += new FrmOrganizationSetup.AddToMainEventHandler(this.frmreg_AddToMain);
            setup.ShowDialog();
        }

        private void treeOrganization_GetNodeDisplayValue(object sender, GetNodeDisplayValueEventArgs e)
        {
            string fieldName = e.Column.FieldName;
            if (fieldName != null)
            {
                if (!(fieldName == "ORG_LEVEL"))
                {
                    if (fieldName == "ORG_LAYER")
                    {
                        e.Value = this.layerData[Convert.ToInt32(e.Value) - 1];
                    }
                    else if (fieldName == "ORG_IS_LEAF")
                    {
                        e.Value = (e.Value.ToString() == "N") ? "否" : "是";
                    }
                    else if (fieldName == "ORG_ACTIVE")
                    {
                        e.Value = (e.Value.ToString() == "N") ? "停用" : "可用";
                    }
                }
                else
                {
                    e.Value = this.levelData[Convert.ToInt32(e.Value) - 1];
                }
            }
            if (e.Column.VisibleIndex == 7)
            {
                string commandtext = "select * from KTSYS_ORG_RGN where ORG_CODE=:ORG_CODE";
                DataParamCol parm = new DataParamCol();
                parm.Add(new InputDataParam("ORG_CODE", e.Value));
                DataTable dt = new DataTable();
                if ((SystemInfo.DbData.Retrieve(commandtext, parm, ref dt) && (dt != null)) && (dt.Rows.Count > 0))
                {
                    e.Value = "已管理";
                }
                else
                {
                    e.Value = "未管理";
                }
            }
        }

        private void treeOrganization_MouseDown(object sender, MouseEventArgs e)
        {
            this.ShowHitInfo(this.treeOrganization.CalcHitInfo(new Point(e.X, e.Y)));
        }
    }
}
